const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ordertype: "1", //1-待付款 2-待收货
    // 订单编号
    orderNumber: "74184450010",
    order_sn: '',
    goods_list: '',
    order_info: ''
  },
  // 复制事件
  copyBtn: function (e) {
    var that = this;
    wx.setClipboardData({
      //准备复制的数据
      data: this.data.order_info.order_sn,
      success: function (res) {
        wx.showToast({
          title: '复制成功',
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options && options.order_sn != '') {
      this.setData({
        order_sn: options.order_sn
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    var that = this,
    login_status = wx.getStorageSync('login_status'),
    order_sn = this.data.order_sn;
    that.setData({
      page: 0,
      order_list: []
    })
    if (!login_status || login_status <= 0) {
      wx.showLoading({
        title: '请先进行登陆',
      })
      setTimeout(() => {
        wx.hideLoading({
          success: (res) => {
            wx.navigateTo({
              url: '/pages/grant/grant',
            })
          },
        })
      }, 1500);
    }else{
      https.post('getOrderInfo', {
        order_sn: order_sn
      }, false, '正在加载').then(res => {
        that.setData({
          order_info: res.order_info,
          goods_list: res.goods_list
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * @xk 取消订单
   * @param {} obj 
   */
  cancel: function (obj) {
    var order_sn = obj.currentTarget.dataset.order_sn;
    https.post('cancelOrder', {
      order_sn: order_sn
    }, false, '正在取消').then(res => {
      this.setData({
        order_list: [],
        page: 0
      })
      this.getOrderList();
    })
  },
  /**
   * @xk 提交支付
   * @param {*} obj 
   */
  goPay: function (obj) {
    var order_sn = obj.currentTarget.dataset.order_sn;
    https.post('goPay', {
      order_sn: order_sn
    }, false, '正在重新支付').then(res => {
      app.globalData.order_sn = res.order_sn;
      if (res.code == 205) {
        //执行微信支付
        wx.requestPayment({
          timeStamp: res.pay_info.timeStamp,
          nonceStr: res.pay_info.nonceStr,
          package: res.pay_info.package,
          signType: res.pay_info.signType,
          paySign: res.pay_info.paySign,
          success(res) {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '支付成功',
                icon: 'none',
                duration: 1500
              })
              wx.redirectTo({
                url: '/pages/pay_success/success',
              })
            } else {
              wx.showToast({
                title: '支付取消',
                icon: 'none',
                duration: 2000
              })
              wx.redirectTo({
                url: '/pages/my_order/my_order',
              })
            }
          },
          fail(res) {
            wx.showToast({
              title: '支付取消 ',
              icon: 'none',
              duration: 1500
            })
            wx.redirectTo({
              url: '/pages/my_order/my_order',
            })
          }
        })
      } else {
        wx.redirectTo({
          url: '/pages/pay_success/success',
        })
      }
    })
  },
  /**
   * @xk 申请退款
   * @param {*} obj 
   */
  backMoney: function (obj) {
    var order_sn = obj.currentTarget.dataset.order_sn;
    wx.navigateTo({
      url: '/pages/applyrefund/applyrefund?order_sn=' + order_sn,
    })
  },
  /**
   * @xk 确认收货
   * @param {*} obj 
   */
  makeSure: function (obj) {
    var order_sn = obj.currentTarget.dataset.order_sn;
    https.post('makeSure', {
      order_sn: order_sn
    }, false, '正在确认收货').then(res => {
      if (res) {
        this.setData({
          order_list: [],
          page: 0
        })
        this.getOrderList();
      }
    })
  },
  common: function (obj) {
    var order_sn = obj.currentTarget.dataset.order_sn;
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?order_sn=' + order_sn,
    })
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
})